package ilender.controller;

import lombok.*;

import com.google.gson.*;

import ilender.annotations.*;
import ilender.dispatching.ControllerTools;
import ilender.mappers.*;
import ilender.models.*;
import ilender.util.*;
import ilender.facebook.*;

/**
 * @see Controller
 */
@Controller("tags")
public class TagsController{
	/**
	 * Generate autocomplete list for categories.
	 *
	 * @param request <pre>
	 * {
	 *     term:(the search string),
	 *     optional preferred_category_id:(category id to put on top)
	 * }
	 * </pre>
	 * @return <pre>
	 * [
	 *     (tag name),
	 *     ...
	 * ]
	 * </pre>
	 */
	public static JsonElement autocomplete(JsonObject request,ControllerTools tools){
		long preferredCategoryId=0;
		try{
			preferredCategoryId=Long.valueOf(request.get("preferred_category_id").getAsString());
		}catch(Exception e){}//If can't parse preferred category id - don't use it!(keep it 0)

		return JsonTools.makeArrayFromIterable(
				tools.session.getMapper(TagsMapper.class).searchTags(
					request.get("term").getAsString(),preferredCategoryId));
	}
}
